<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/spring%E8%8E%B7%E5%8F%96-maven-profile/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">92 字 ~1分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<p><a href="https://www.cnblogs.com/zeng1994/p/06917ed3b98677fa7a1b0f74de16c3be.html">https://www.cnblogs.com/zeng1994/p/06917ed3b98677fa7a1b0f74de16c3be.html</a></p>

<p>使用maven的profile功能，我们可以实现多环境配置文件的动态切换，可参考我的上一篇博客。但随着SpringBoot项目越来越火，越来越多人喜欢用SpringBoot的profile功能。但是用SpringBoot的profile功能时，一般我们默认激活的profile肯定是开发环境的profile。当我们打成jar包后，如果在生产环境下运行，就需要在运行这个jar包的命令后面加个命令行参数来指定切换的profile。虽然这样很方便，但是容易忘记加这个参数。</p>

<p>我们可以通过maven的profile功能和SpringBoot的profile功能结合使用。效果为：当maven打包时通过profile指定配置为test环境的配置，那么我们SpringBoot里面默认激活的就是test环境的配置。 这样我们只需要打包时指定profile后，直接运行jar就可以，不需要在命令行加参数了。这个效果就和我们普通web项目使用maven的profile的效果类似了。</p>

<h2 id="一-思路">一、思路</h2>

<p>（1）通过maven的profile功能，在打包的时候，通过-P指定maven激活某个pofile，这个profile里面配置了一个参数activatedProperties，不同的profile里面的这个参数的值不同</p>

<p>（2）SpringBoot的application.properties文件里面spring.profiles.active填的值取上面maven的activatedProperties参数值。</p>

<p>这样能实现的效果为：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">示例一</span><span style="color:#a40000">：</span>
    <span style="color:#000">maven打包命令为</span>   <span style="color:#000">mvn</span> <span style="color:#000">clean</span> <span style="color:#204a87;font-weight:bold">package</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">P</span> <span style="color:#000">test</span>    
    <span style="color:#000">那么application</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">properties里面的spring</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">profiles</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">active值就是maven中</span> <span style="color:#000">id为test的profile的activatedProperties参数值</span>
<span style="color:#000">示例二</span><span style="color:#a40000">：</span>
    <span style="color:#000">maven打包命令为</span>   <span style="color:#000">mvn</span> <span style="color:#000">clean</span> <span style="color:#204a87;font-weight:bold">package</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">P</span> <span style="color:#000">product</span>
    <span style="color:#000">那么application</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">properties里面的spring</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">profiles</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">active值就是maven中</span> <span style="color:#000">id为product的profile的activatedProperties参数值</span></code></pre></div>
<h2 id="二-案例">二、案例</h2>

<h5 id="1-项目结构介绍">（1）项目结构介绍</h5>

<p>项目结构如下图所示，是个常见的SpringBoot项目结构，不同环境的propertis文件的后缀不同（见图中红框处）</p>

<p><img src="https://images2018.cnblogs.com/blog/1222688/201808/1222688-20180829143534322-1692462961.png" alt="img" /></p>

<h5 id="2-pom文件中配置maven的profile">（2）pom文件中配置maven的profile</h5>

<p>maven的profile的配置见下面代码</p>

<p>注意：maven的profile中activatedProperties参数值需要和SpringBoot的不同环境Properties文件的后缀一样。</p>

<p>比如开发环境的Properties的文件名为application-develop.properties，那么maven中develop的profile里面的activatedProperties参数值就应该是develop</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">    &lt;profiles&gt;
        &lt;profile&gt;
        	&lt;!-- 开发 --&gt;
            &lt;id&gt;develop&lt;/id&gt;
            &lt;activation&gt;
                &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
            &lt;/activation&gt;
            &lt;properties&gt;
                &lt;activatedProperties&gt;develop&lt;/activatedProperties&gt;
            &lt;/properties&gt;
        &lt;/profile&gt;
        &lt;profile&gt;
        	&lt;!-- 测试 --&gt;
            &lt;id&gt;fuy&lt;/id&gt;
            &lt;properties&gt;
                &lt;activatedProperties&gt;fuy&lt;/activatedProperties&gt;
            &lt;/properties&gt;
        &lt;/profile&gt;
        &lt;profile&gt;
        	&lt;!-- 生产 --&gt;
            &lt;id&gt;production&lt;/id&gt;
            &lt;properties&gt;
                &lt;activatedProperties&gt;production&lt;/activatedProperties&gt;
            &lt;/properties&gt;
        &lt;/profile&gt;
    &lt;/profiles&gt;</code></pre></div>
<h5 id="3-application-properties中的配置">（3）application.properties中的配置</h5>

<p>在application.properties文件中配置SpringBoot默认激活的propertis文件。这时候spring.profiles.active取上面maven的profile里面配置的activatedProperties的值，这个取值要用@符号来取。具体见下面代码</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">spring.profiles.active=${activatedProperties}</code></pre></div>
<h5 id="4-如何打包">（4）如何打包</h5>

<p>打包时用 mvn clean package <strong>-P profile的id</strong></p>

<p>​    如果不加-P参数，那么默认就是<activeByDefault>true</activeByDefault>所在的profile</p>

<h5 id="5-效果图">（5）效果图</h5>

<p>​    当我们打包命令为<strong>mvn clean package -P production</strong> 时，解压后的jar包中application.properties配置文件中spring.profiles.active的值自动变成了production</p>

<p>​    <img src="https://images2018.cnblogs.com/blog/1222688/201808/1222688-20180829143536252-1911586796.png" alt="img" /></p>

<h2 id="三-小结">三、小结</h2>

<p>（1）该方式优点：打包后不需要通过命令行参数来切换不同环境的配置文件，把指定环境的这一步放到了maven打包的命令上</p>

<p>（2）该方式其实是利用了maven的profile功能和SpringBoot的profile相结合使用</p>

<h2 id="四-参考链接">四、参考链接</h2>

<p>（1）<a href="https://www.cnblogs.com/zeng1994/p/a442108012ffd6a97b22c63055b48fe9.html">https://www.cnblogs.com/zeng1994/p/a442108012ffd6a97b22c63055b48fe9.html</a></p>

<p>（2）<a href="http://dolszewski.com/spring/spring-boot-properties-per-maven-profile/">http://dolszewski.com/spring/spring-boot-properties-per-maven-profile/</a></p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/spring%E8%87%AA%E5%B8%A6%E7%9A%84%E7%BA%BF%E7%A8%8B%E6%B1%A0threadpooltaskexecutor/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/ssh%E8%BF%9C%E7%A8%8B%E6%89%A7%E8%A1%8C%E5%91%BD%E4%BB%A4%E5%87%BA%E7%8E%B0are-you-sure-you-want-to-continue-connecting-yesno%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>